package com.zzg.redis.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import com.zzg.redis.util.RedisPool;
import com.zzg.redis.util.RedisUtil;

import redis.clients.jedis.JedisPoolConfig;


@Configuration
@PropertySource(value= {"classpath:redis.properties"})
public class RedisConfig {
		@Value("${redis.setting.maxTotal}")
		private int maxTotal;
		@Value("${redis.setting.maxIdle}")
		private int maxIdle;
		@Value("${redis.setting.minIdle}")
		private int minIdle;
		@Value("${redis.setting.minEvictableIdleTimeMillis}")
		private long minEvictableIdleTimeMillis;
		@Value("${redis.setting.serverIp}")
		private String serverIp;
		@Value("${redis.setting.port}")
		private int port;
		@Value("${redis.setting.timeout}")
		private int timeout;
		@Value("${redis.setting.password}")
		private String password;
		
	
		// 获取JedisPoolConfig 连接工厂对象
		@Bean
		public JedisPoolConfig getJedisPoolConfig() {
			JedisPoolConfig config = new JedisPoolConfig();
			config.setMaxTotal(maxTotal);
			config.setMaxIdle(maxIdle);
			config.setMinIdle(minIdle);
			config.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
			return config;
		}
		
		// 获取RedisPool连接对象
		@Bean(initMethod="init")
		public RedisPool getRedisPool() {
			RedisPool pool = new RedisPool();
			pool.setServerIp(serverIp);
			pool.setPort(port);
			pool.setTimeout(timeout);
			pool.setPassword(password);
			pool.setConfig(getJedisPoolConfig());
			return pool;
		}
		// 获取RedisUtil连接对象
		@Bean
		public RedisUtil getRedisUtil(){
			RedisUtil util = new RedisUtil();
			util.setRedisPool(getRedisPool());
			return util;
		}
		
		
		
}
